package com.siwei.mes.controller.erp;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.service.laboratory.CustomInfoService;
import com.siwei.mes.service.sync.erp.ErpCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 客户信息表相关
 *
 * @author linzi
 * @since 2024-08-13 21:07:57
 */
@Api(value = "客户信息表")
@RestController
@RequestMapping("erpCustomer")
public class ErpCustomerController {
    @Resource
    private ErpCustomerService erpCustomerService;
    @Resource
    private CustomInfoService customInfoService;

    @ApiOperation(value = "上传到协会")
    @PostMapping("/uploadLaboratory")
    public Json uploadLaboratory(@RequestBody ErpCustomer entity) {
        assertTrue(entity.getId() == null, "客户主键id为空");
        ErpCustomer erpCustomer = erpCustomerService.loadById(entity.getId());
        if (erpCustomer != null) {
            /**这里操作协会的*/
            customInfoService.saveOrUpdate(erpCustomer, true, true);
        }
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "修改客户信息表")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) ErpCustomer entity) {
        ErpCustomer erpCustomer = erpCustomerService.update(entity);
        if (erpCustomer != null) {
            /**这里操作协会的*/
            customInfoService.saveOrUpdate(erpCustomer, false, true);
        }
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取客户信息表")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<ErpCustomer, ErpCustomer> query) {
        return Json.succ().data(erpCustomerService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取客户信息表")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(erpCustomerService.loadById(id));
    }

    @ApiOperation(value = "获取所有客户信息表")
    @PostMapping("getAll")
    public Json getAll(@RequestBody ErpCustomer entity) {
        return Json.succ().data(ApiResult.addList(erpCustomerService.getAll(entity)));
    }

}
